<template>
	<view class="detail">
		<view class="detail-title">
			{{formData.title}}
		</view>
		<view class="detail-header">
			<view class="detail-header__logo">
				<image :src="formData.author.avatar"></image>
			</view>
			<view class="detail-header__content">
				<view class="detail-header__content-title">
					{{formData.author.author_name}}
				</view>
				<view class="detail-header__content-info">
					<text>{{formData.create_time}}</text>
					<text>{{formData.browse_count}}浏览</text>
					<text>{{formData.thumbs_up_count}}赞</text>
				</view>
			</view>
			<!-- 关注 -->
			<button type="default" class="detail-header__button" @click="follow(formData.author.id)">{{formData.is_author_like ? '取消关注' : '关注'}}</button>
		</view>
		<view class="detail-content">
			<view class="detail-html">
				<u-parse :content="formData.content" :noData="noData"></u-parse>
				<!-- <rich-text :nodes="formData.content"></rich-text> -->
			</view>
			<view class="detail-comment">
				<view class="comment-title">最新评论</view>
				<view class="comment-content" v-for="item in commentsList" :key="item.comment_id">
					<!-- 评论组件 -->
					<comment-box :comments="item" @reply="reply"/>
				</view>
			</view>
		</view>
		<!-- 底部 -->
		<view class="detail-bottom">
			<view class="detail-bottom__input" @click="openComment">
				<text>谈谈你的看法</text>
				<uni-icons type="compose" size="16" color="#F07373"></uni-icons>
			</view>
			<view class="detail-bottom__icons">
				<view class="detail-bottom__icons-box" @click="open">
					<uni-icons type="chat" size="22" color="#F07373"></uni-icons>
				</view>
				<view class="detail-bottom__icons-box" @click="likeTap">
					<uni-icons :type="formData.is_like ? 'heart-filled' : 'heart'" size="22" color="#F07373"></uni-icons>
				</view>
				<view class="detail-bottom__icons-box" @click="thumbsup">
					<uni-icons :type="formData.is_thumbs_up ? 'hand-thumbsup-filled' : 'hand-thumbsup'" size="22" color="#F07373"></uni-icons>
				</view>
			</view>
		</view>
		<!-- 弹出层 -->
		<uni-popup type="bottom" ref="popup" :maskClick="false">
			<view class="popup-wrapper">
				<view class="popup-header">
					<text class="popup-header__item" @click="popupClose">取消</text>
					<text class="popup-header__item" @click="submitComment">发布</text>
				</view>
				<view class="popup-content">
					<textarea class="popup-textarea" v-model="commentsVal" placeholder="请输入评论内容" fixed maxlength="200" />
					<view class="popup-count">{{commentsVal.length}}/200</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uParse from "@/components/feng-parse/parse.vue"
	import {
		get_detail, 
		update_comment, 
		get_comments,
		update_author,
		update_like,
		update_thumbsup
	} from '@/common/api/index.js'
	export default {
		data() {
			return {
				// 传过来的参数
				formData: {},
				noData: '<p style="color:#999;text-align:center;font-size:28upx">数据请求中...</p>',
				// 评论内容的值
				commentsVal: '',
				// 评论信息数据数组
				commentsList: [],
				// 回复评论相关信息对象
				replyFormData: {}
			}
		},
		onLoad(options) {
			this.formData = JSON.parse(options.prams)
			
			// 调用请求获取文章信息
			this.getDetail()
			// 调用请求获取文章评论信息
			this.getComments()
		},
		methods: {
			// 获取指定文章
			getDetail() {
				get_detail({
					article_id: this.formData._id
				}).then(res => {
					console.log('指定文章',res)
					this.formData = res.data
				})
			},
			// 评论请求
			setUpdateComment() {
				update_comment({
					article_id: this.formData._id,  //文章id
					content: this.commentsVal  ,//评论内容
					comment_id: this.replyFormData.comment_id ,//评论id
					replay_id: this.replyFormData.replay_id, //子回复id
					is_reply: this.replyFormData.is_reply //是否子回复
				})
				.then(res => {
					console.log('评论：',res)
					uni.showToast({
						title: '评论成功！'
					})
					// 刷新评论
					this.getComments()
					// 清空文本域
					this.commentsVal = ''
					// 清空
					this.replyFormData = {}
					// 关闭popup
					this.popupClose()
				})
				.catch(err => {
					uni.showToast({
						title: '评论失败：' + err,
						icon: 'none'
					})
				})
			},
			// 获取评论
			getComments() {
				get_comments({
					article_id: this.formData._id
				})
				.then(res => {
					console.log('评论信息', res)
					this.commentsList = res.data
				})
			},
			// 打开评论框
			openComment() {
				this.$refs.popup.open()
			},
			// 关闭评论框
			popupClose() {
				// 关闭popup
				this.$refs.popup.close()
			},
			// 提交评论
			submitComment() {
				if(!this.commentsVal) return uni.showToast({
					title: '请输入评论内容',
					icon: 'none'
				})
				console.log('发布')
				// 调用评论函数
				this.setUpdateComment()
			},
			// 回复评论
			reply(e) {
				// console.log(e)
				this.replyFormData = {
					comment_id: e.comments.comment_id,
					is_reply: e.is_reply
				}
				if(e.comments.replay_id) {
					this.replyFormData.replay_id = e.comments.replay_id
				}
				// 打开popup
				this.openComment()
				
				console.log(this.replyFormData)
			},
			// 关注作者
			follow(author_id) {
				this.setFollow(author_id)
			},
			// 关注作者请求函数
			setFollow(author_id) {
				update_author({
					author_id
				}).then(res => {
					console.log('关注', res)
					this.formData.is_author_like = !this.formData.is_author_like
					uni.$emit('update_follow_author')
					uni.showToast({
						title: this.formData.is_author_like ? '关注成功' : '取消关注',
						icon: 'none'
					})
				})
			},
			// 收藏文章请求函数
			setUpdateLike() {
				update_like({
					article_id: this.formData._id
				}).then(res => {
					this.formData.is_like = !this.formData.is_like
					uni.showToast({
						title: '点赞成功',
						icon: 'none'
					})
					// 通知
					uni.$emit('update_article', this.formData._id)
				})
			},
			// 收藏按钮点击事件
			likeTap() {
				this.setUpdateLike()
			},
			// 点赞请求函数
			setThumbsup() {
				update_thumbsup({
					article_id: this.formData._id
				}).then(res => {
					this.formData.is_thumbs_up = true
					this.formData.thumbs_up_count ++
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				})
			},
			// 点赞
			thumbsup() {
				this.setThumbsup()
			},
			// 打开评论列表
			open() {
				uni.navigateTo({
					url: '../detail-comments/detail-comments?id=' + this.formData._id,
					
				})	
			}
		
		},
		components:{
				uParse
		}
	}
</script>

<style lang="scss" scoped>
	page{}
	.detail{
		padding-top: 30upx;
		padding-bottom: 88upx;
		.detail-title{
			padding: 0 30upx;
			font-size: 36upx;
			font-weight: bold;
			color: #333;
			
			/* #ifndef MP-WEIXIN */
			overflow: hidden;			text-overflow: ellipsis;			display: -webkit-box;			-webkit-line-clamp: 2;			-webkit-box-orient: vertical; 			/* #endif */
			
			/* #ifdef MP-WEIXIN */
				text-overflow: -o-ellipsis-lastline;				overflow: hidden;				text-overflow: ellipsis;				display: -webkit-box;				-webkit-line-clamp: 2;				-webkit-box-orient: vertical;				align-content: center;			/* #endif */
			
		}
		.detail-header{
			display: flex;
			align-items: center;
			margin-top: 20upx;
			padding: 0 30upx;
			.detail-header__logo{
				flex-shrink: 0;
				width: 80upx;
				height: 80upx;
				border-radius: 50%;
				overflow: hidden;
				image{
					width: 100%;
					height: 100%;
				}
			}
			.detail-header__content{
				width: 100%;
				padding-left: 20upx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				font-size: 24upx;
				.detail-header__content-title{
					font-size: 28upx;
					color: #333;
				}
				.detail-header__content-info{
					color: #999;
					text{
						margin-right: 20upx;
					}
				}
			}
			.detail-header__button{
				flex-shrink: 0;
				height: 60upx;
				line-height: 60upx;
				font-size: 24upx;
				color: #fff;
				background-color: $mk-base-color;
				padding-right: 28upx;
				padding-left: 28upx;
				box-sizing: border-box;
				border-radius: 10upx;
			}
		}
		.detail-content{
			padding: 30upx 0;
			.detail-html{
				padding: 0 30upx;
			}
			.detail-comment{
				margin-top: 60upx;
				.comment-title{
					padding: 20upx 30upx;
					font-size: 28upx;
					color: #666;
					border-bottom: 1px solid #f5f5f5;
				}
				.comment-content{
					padding: 0 30upx;
					border-top: 1px solid #eee;
				}
			}
		}
		.detail-bottom{
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			width: 100%;
			height: 88upx;
			display: flex;
			align-items: center;
			border-top: 1px solid #f5f5f5;
			background-color: #fff;
			box-sizing: border-box;
			.detail-bottom__input{
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-left: 20upx;
				width: 100%;
				height: 60upx;
				border: 1px solid #ddd;
				padding: 0 20upx;
				border-radius: 10upx;
				text{
					font-size: 28upx;
					color: #999;
				}
			}
			.detail-bottom__icons{
				display: flex;
				flex-shrink: 0;
				padding: 0 20upx;
				.detail-bottom__icons-box{
					position: relative;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 88upx;
				}
			}
		}
		.popup-wrapper{
			background-color: #fff;
			.popup-header{
				display: flex;
				justify-content: space-between;
				font-size: 28upx;
				color: #666;
				border-bottom: 1px solid #F5F5F5;
				.popup-header__item{
					height: 100upx;
					line-height: 100upx;
					padding: 0 30upx;
				}
			}
			.popup-content{
				width: 100%;
				padding: 30upx;
				box-sizing: border-box;
				.popup-textarea{
					width: 100%;
					height: 400upx;
				}
				.popup-count{
					display: flex;
					justify-content: flex-end;
					font-size: 24upx;
					color: #999;
				}
			}
		}
	}
</style>
